#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:Zhangcl
# import gevent
# def func1():
#     print('\033[31;1m111\033[0m')
#     gevent.sleep(2)
#     print('\033[31;1m222\033[0m')
# def func2():
#     print('\033[32;1m333.\033[0m')
#     gevent.sleep(1)
#     print('\033[32;1m444\033[0m')
# def func3():
#     print('\033[32;1m555\033[0m')
#     gevent.sleep(0)
#     print('\033[32;1m666\033[0m')
# gevent.joinall([
#     gevent.spawn(func1),
#     gevent.spawn(func2),
#     gevent.spawn(func3),
# ])
from gevent import monkey
import gevent
from  urllib.request import urlopen
import time
monkey.patch_all()

def f(url):
    print('GET: %s' % url)
    resp = urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

urls = ['http://www.sina.com.cn/',
        'http://www.163.com/',
        'http://www.qq.com/',
        ]
time_start = time.time()
for i in urls:
    f(i)
print('同步：',time.time()-time_start)

start_time = time.time()
gevent.joinall([
    gevent.spawn(f, 'http://www.sina.com.cn/'),
    gevent.spawn(f, 'http://www.163.com/'),
    gevent.spawn(f, 'http://www.qq.com/'),
])
print('异步：',time.time()-start_time)